package com.cs.archivesmanagement.serviceimpl;

import com.cs.archivesmanagement.dao.EntityDao;
import com.cs.archivesmanagement.entity.*;
import com.cs.archivesmanagement.service.ArchivesDealWithService;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * Created by amo on 2015/8/24.
 */

@Repository
public class ArchivesDealWithServiceImpl implements ArchivesDealWithService {

    @Resource
    EntityDao entityDao;


    @Override
    public AmArchivesRequests findById(Object id) {
        return (AmArchivesRequests)entityDao.getEntity(AmArchivesRequests.class,id);
    }

    @Override
    public Integer dealWithRequest(Integer requestId, boolean approvalOrdenied, Integer approvalerId) {


        //根据主键查找请求实体
        AmArchivesRequests amArchivesRequests;
        amArchivesRequests=(AmArchivesRequests) entityDao.getEntity(AmArchivesRequests.class,requestId);

        //查找审核者的信息，赋予实体

        AmAccount amAccount;
        amAccount=(AmAccount)entityDao.getEntity(AmAccount.class,approvalerId);

        amArchivesRequests.setArchivesRequestsArchiveApprovalUserId(amAccount);

        //被审核的档案状态更改

        //申请被通过
        if(approvalOrdenied){
            amArchivesRequests.setArchivesRequestsStatus(AmArchivesRequests.archivesRequestsStatus_approvaled);

            //同意增加档案
              if(amArchivesRequests.getArchivesRequestsType()==AmArchivesRequests.archivesRequestsType_add){

                  amArchivesRequests.getArchivesRequestsArchiveId().setArchivesStatus(AmArchives.archivesStatus_vaild);

                  entityDao.updateEntity(amArchivesRequests.getArchivesRequestsArchiveId());
                  entityDao.updateEntity(amArchivesRequests);

              }

              //同意修改档案
            else if(amArchivesRequests.getArchivesRequestsType()==AmArchivesRequests.archivesRequestsType_modify){

                  //新修改的档案设为有效
                  amArchivesRequests.getArchivesRequestsArchiveId().setArchivesStatus(AmArchives.archivesStatus_vaild);

                  //将以前的档案设为无效
                  AmArchives parentArchive= (AmArchives) entityDao.getEntity(AmArchives.class,amArchivesRequests.getArchivesRequestsArchiveId().getArefreceParentId());
                  parentArchive.setArchivesStatus(AmArchives.archivesStatus_invaild);


                  entityDao.updateEntity(parentArchive);
                  entityDao.updateEntity(amArchivesRequests.getArchivesRequestsArchiveId());
                  entityDao.updateEntity(amArchivesRequests);

              }

              // 同意删除档案
              else if(amArchivesRequests.getArchivesRequestsType()==AmArchivesRequests.archivesRequestsType_delete){

                  //删除这个档案，状态变为无效
                  amArchivesRequests.getArchivesRequestsArchiveId().setArchivesStatus(AmArchives.archivesStatus_invaild);

                  entityDao.updateEntity(amArchivesRequests.getArchivesRequestsArchiveId());
                  entityDao.updateEntity(amArchivesRequests);


              }



              //同意借阅档案
            else{

                  System.out.println("同意借阅档案");
                  AmArchivesPriority amArchivesPriority=new AmArchivesPriority();
                  amArchivesPriority.setArchivesPriorityArchiveId(amArchivesRequests.getArchivesRequestsArchiveId());
                  amArchivesPriority.setArchivesPriorityUserId(amArchivesRequests.getArchivesRequestsUserId());
                  amArchivesPriority.setArchivesPriorityTimeBagain(amArchivesRequests.getArchivesRequestsArchiveTime());
                  amArchivesPriority.setArchivesPriorityTimeTails(amArchivesRequests.getArchivesRequestsArchiveEndTime());

                  entityDao.addEntity(amArchivesPriority);
                  entityDao.updateEntity(amArchivesRequests);
              }


        }

        else{

            //审核状态变为拒绝

            amArchivesRequests.setArchivesRequestsStatus(AmArchivesRequests.archivesRequestsStatus_denied);

            //拒绝增加档案
            if(amArchivesRequests.getArchivesRequestsType()==AmArchivesRequests.archivesRequestsType_add){

                entityDao.updateEntity(amArchivesRequests);

            }

            //拒绝修改档案
            else if(amArchivesRequests.getArchivesRequestsType()==AmArchivesRequests.archivesRequestsType_modify){

                entityDao.updateEntity(amArchivesRequests);


            }

            //拒绝删除档案
            else if(amArchivesRequests.getArchivesRequestsType()==AmArchivesRequests.archivesRequestsType_delete){


                entityDao.updateEntity(amArchivesRequests);


            }

            //拒绝借阅档案

            else{
                entityDao.updateEntity(amArchivesRequests);


            }


        }


        return  1;
    }


  /*  private int archivesRequestsId;
    private Timestamp archivesRequestsArchiveEndTime;
    private Timestamp archivesRequestsArchiveTime;
    private Integer archivesRequestsStatus;
    private Integer archivesRequestsType;
    private AmAccount archivesRequestsArchiveApprovalUserId;
    private AmArchives archivesRequestsArchiveId;
    private AmAccount archivesRequestsUserId;*/

    @Override
    public ArrayList<AmArchivesRequests> getAllAmArchivesRequestsByParameter(Integer TypeId,Integer accountId, Integer archivesStatus) {
        ArrayList<Criterion> criterions=new ArrayList<>();

        if(accountId!=null) {
            AmAccount amAccount = new AmAccount();
            amAccount.setAccountId(accountId);
            criterions.add(Restrictions.eq("archivesRequestsUserId", amAccount));
        }

        if(TypeId!=null) {
            criterions.add(Restrictions.eq("archivesRequestsType", TypeId));
        }

        if(archivesStatus!=null) {

            criterions.add(Restrictions.eq("archivesRequestsStatus", archivesStatus));

        }
     return  (ArrayList<AmArchivesRequests>)entityDao.getEntityList(AmArchivesRequests.class,criterions);

    }

    @Override
    public Integer rollBackRequest(Integer amArchivesRequestsId) {


        System.out.println("回滚操作");

        AmArchivesRequests amArchivesRequests=(AmArchivesRequests)entityDao.getEntity(AmArchivesRequests.class,amArchivesRequestsId);

        System.out.println("获取实体成功");

        amArchivesRequests.getArchivesRequestsArchiveId().setArchivesStatus(AmArchives.archivesStatus_invaild);


      AmArchives parent=null;


              if(amArchivesRequests.getArchivesRequestsArchiveId().getArefreceParentId()!=null&&amArchivesRequests.getArchivesRequestsArchiveId().getArefreceParentId()!=-1) {

                  System.out.println("准备获取父母archives实体");

                  parent= (AmArchives) entityDao.getEntity(AmArchives.class, amArchivesRequests.getArchivesRequestsArchiveId().getArefreceParentId());
                  System.out.println("准备获取父母archives111111111");

              }

        //如果有上一个版本，回滚到上一个版本
      if(parent!=null){

          System.out.println("准备获取父母archives实体成功"+parent.getArchivesId());
          parent.setArchivesStatus(AmArchives.archivesStatus_vaild);
          entityDao.updateEntity(parent);

      }

        System.out.println("更新文档状态");

        //更新当前版本的档案 设为无效
        entityDao.updateEntity(amArchivesRequests.getArchivesRequestsArchiveId());

        System.out.println("删除这个请求");

        amArchivesRequests.setArchivesRequestsStatus(AmArchivesRequests.archivesRequestsStatus_rollback);
        //删除
        entityDao.updateEntity(amArchivesRequests);


        return 1;
    }


}
